__author__ = 'liyuan_t'
import urllib2
import urllib
import re
import random
import os
from qiniu import Auth, put_data, BucketManager

accessKey = "";
secrectKey = "";
bucketName = "markdown";

q = Auth(accessKey, secrectKey);


def GetPage(i):
    url = "http://www.lofter.com/dwr/call/plaincall/TagBean.search.dwr";
    headers = {
        "Accept": "*/*",
        "Accept-Encoding": "deflate",
        "Accept-Language": "zh-CN,zh;q=0.8",
        "Content-Type": "text/plain",
        "Cookie": "usertrack=c+xxC1YR8hI6ifxBHWbdAg==; NTESLOFTSI=94C36FD654643F197F9D86B7DB885CAA.lofter19-8010; firstentry=%2Flogin.do%3FX-From-ISP%3D2|https%3A%2F%2Fwww.baidu.com%2Flink%3Furl%3DWY4DGkEelUtQuM3vCPJhiReHs0UytuuGI8bZqfg998u%26wd%3D%26eqid%3Dce5d47a300006f2c000000055628d7a9; JSESSIONID-WLF-XXD=8438dd48439297e972bc97a3a5317c76726cdfc7a23b71c1076bcaf6ee56ca617287cf8bbaadce4042bcc0ed85682768adae5b777eeb61aae67c9addfb5ba694a23842e674a7fd59f83ef0ad683e7deea07834392a9703a847099c84f3878b4c9653d95b7ca78e0431dfb4d740599b59aa164db51f5e789607227d1bbf05e5eece984300; fastestuploadproxydomainkey=uploadgz|1445517230190; _ntes_nnid=abf9a7a48decdd133ab379a6bdd7d46a,1445517231078; Hm_lvt_c5c55f9c94fbca8efd7d891afb3210e8=1445517243; Hm_lpvt_c5c55f9c94fbca8efd7d891afb3210e8=1445517243; reglogin_doopen=1; reglogin_isLoginFlag=; __utma=61349937.713601222.1445517244.1445517244.1445517311.2; __utmb=61349937.2.10.1445517311; __utmc=61349937; __utmz=61349937.1445517311.2.2.utmcsr=baidu|utmccn=(organic)|utmcmd=organic|utmctr=lofter; LOFTER_SESS=P2-yULlSSRqWtDeli2jTFYpbgnKia1jHdYsqSGUvs1ImRR4yX03klruNnSb9yZycxOGI0MXISmU2%0A3bceng1FMc1h1T_px94M-b5pIPtaexXbG_XIMGq1CA%3D%3D; NETEASE_WDA_UID=494713191#|#1445517386093; noAdvancedBrowser=0",
        "DNT": "1",
        "Referer": "http://www.lofter.com/tag/%E5%A5%B3%E7%A5%9E?first=1d44e4d3_89fe165",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.71 Safari/537.36"
    };
    data = """callCount=1
scriptSessionId=${scriptSessionId}187
httpSessionId=
c0-scriptName=TagBean
c0-methodName=search
c0-id=0
c0-param0=string:%E5%A5%B3%E7%A5%9E
c0-param1=number:0
c0-param2=string:
c0-param3=string:excellent
c0-param4=boolean:false
c0-param5=number:0
c0-param6=number:20
c0-param7=number:""" + i + """
c0-param8=number:0
batchId=559377
    """;
    req = urllib2.Request(url, headers=headers, data=data);
    content = urllib2.urlopen(req).read();
    if isinstance(content, unicode):
        pass;
    else:
        content = content.decode('utf-8');
        CONTENT = content.decode('utf-8');
    return content;


def GetUrl(content):
    pattern = re.compile(r'homePageUrl\=\".*?\"');
    match = pattern.findall(content);
    urls = [];
    if match:
        for m in match:
            urls.append(m.split("\"")[1]);
    return urls;


def GetRss(url):
    url = url + "/rss";
    req = urllib2.Request(url);
    content = urllib2.urlopen(req).read();
    if isinstance(content, unicode):
        pass;
    else:
        content = content.decode('utf-8');

    return content;


def GetPicUrl(content):
    pattern = re.compile(r'http\:\/\/img.*?\"');
    match = pattern.findall(content);
    pics = [];
    if match:
        for m in match:
            m = m.replace('"', '');
            if m.find("500x0") != -1:
                m = m.replace("500x0", "1600x0");
            pics.append(m);
    return pics;


def DownPic(url, name, path):
    key = 'lofter.img\\' + path + "\\" + name + ".jpg";
    bucket = BucketManager(q);
    ret,info = bucket.stat(bucketName,key);
    if ret == None:
        data = urllib2.urlopen(url).read();
        token = q.upload_token(bucketName);
        ret, info = put_data(token, key, data);
        print info
    else:
        return;

def Go(i):
    content = GetPage(i);
    urls = GetUrl(content);
    for url in urls:
        rss = GetRss(url);
        pics = GetPicUrl(rss);
        print url, len(pics);
        j = 0;
        for pic in pics:
            try:
                # print("\t" + pic)
                name = url[7:];
                path = name;
                DownPic(pic, str(j), path);
                j = j + 1;
            except Exception, e:
                print e;
                continue;


for i in range(0, 2000, 20):
    Go(str(i));
